package com.guangyuan.network.interval;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.guangyuan.network.constants.SqlType;
import com.guangyuan.network.dao.DBFactory;
import com.guangyuan.network.dao.interfaces.sql.ProductSql;
import com.guangyuan.network.dataobject.Product;
import com.guangyuan.network.datasource.pool.util.ConnectionUtil;
import com.guangyuan.network.external.ProductOper;

public class ProductCache extends DefaultCache implements ProductOper {

	private static ProductCache productCache;

	@Override
	public boolean deleteProduct(int id) {
		String[] parameters = new String[] { "" + id };

		Integer result = (Integer) execute(parameters, ProductSql.delete,
				SqlType.OTHER);

		if (result > 0) {
			return true;
		}
		return false;
	}

	@Override
	public int insertProduct(Product product) {
		String[] parameters = new String[6];
		parameters[0] = product.getName();
		parameters[1] = product.getCnName();
		parameters[2] = product.getDescription();
		parameters[3] = product.getOperator();
		parameters[4] = product.getProductGroupId().toString();
		parameters[5] = product.getImgLink();

		Integer result = (Integer) execute(parameters, ProductSql.insert,
				SqlType.INSERT);
		return result;
	}

	@Override
	public List<Product> listProductByProductGroupId(int productGroupId,
			int start, int end) {
		Object[] parameters = new Object[] { productGroupId, start, end };

		Connection conn = ConnectionUtil.getConnection();

		ResultSet rs = (ResultSet) DBFactory.getDefaultDao().executeSql(conn,
				ProductSql.selectByProductGroupId, parameters, SqlType.OTHER);

		List<Product> list = new ArrayList<Product>();

		if (null != rs) {
			try {
				while (rs.next()) {
					Product product = new Product();
					product.setId(rs.getInt(1));
					product.setName(rs.getString(2));
					product.setCnName(rs.getString(3));
					product.setDescription(rs.getString(4));
					product.setGmtCreate(rs.getDate(5));
					product.setGmtModified(rs.getDate(6));
					product.setOperator(rs.getString(7));
					product.setProductGroupId(rs.getInt(8));
					product.setImgLink(rs.getString(9));
					list.add(product);
				}
			} catch (SQLException e) {
				logger.error(e.getMessage());
			} finally {
				ConnectionUtil.closeConnection(conn);
			}
		}

		return list;
	}

	@Override
	public boolean updateProduct(Product product) {
		String[] parameters = new String[6];
		parameters[0] = product.getName();
		parameters[1] = product.getCnName();
		parameters[2] = product.getDescription();
		parameters[3] = product.getOperator();
		parameters[4] = product.getImgLink();
		parameters[5] = product.getId().toString();

		Integer result = (Integer) execute(parameters, ProductSql.update,
				SqlType.OTHER);

		if (result > 0) {
			return true;
		}

		return false;
	}

	public static ProductCache getInstance() {
		if (null == productCache) {
			productCache = new ProductCache();
		}
		return productCache;
	}

	@Override
	public Product getProductById(int id) {
		Object[] parameters = new Object[] {id};
		Connection conn = ConnectionUtil.getConnection();

		ResultSet rs = (ResultSet) DBFactory.getDefaultDao().executeSql(conn,
				ProductSql.getProduct, parameters, SqlType.OTHER);
		Product product = null;
		if (null != rs) {
			try {
				if (rs.next()) {
					product = new Product();
					product.setId(rs.getInt(1));
					product.setName(rs.getString(2));
					product.setCnName(rs.getString(3));
					product.setDescription(rs.getString(4));
					product.setGmtCreate(rs.getDate(5));
					product.setGmtModified(rs.getDate(6));
					product.setOperator(rs.getString(7));
					product.setProductGroupId(rs.getInt(8));
					product.setImgLink(rs.getString(9));
				}
			} catch (SQLException e) {
				logger.error(e.getMessage());
			} finally {
				ConnectionUtil.closeConnection(conn);
			}
		}
		return product;
	}
}
